<?php

/**
 * View helper to display admin menu in collapsed state
 * @package Am_View
 */
class Am_View_Helper_AdminMenuCollapsed extends Zend_View_Helper_Abstract {
    protected $activePageId = null;
    protected $acl = null;

    public function setAcl($acl)
    {
        $this->acl = $acl;
    }

    public function adminMenuCollapsed() {
        return $this;
    }

    public function renderMenu(Zend_Navigation_Container $container, $options = array()) {
        $html = '';
        $pages = $container->getPages();
        foreach ($pages as $page)
        {
            /* @var $page Zend_Navigation_Page */

            if ($this->acl && $page->getResource())
                if (!$this->acl->hasPermission($page->getResource(), $page->getPrivilege()))
                    continue;
            if ($page->isActive() ) {
                $this->activePageId = $this->getId($page);
            }
            if (!$page->isVisible(true)) continue;
            if (!$page->getHref()) continue;
            $subMenu = $this->renderSubMenu($page, $page->getLabel());
            $class = $subMenu ? 'folder' : '';

            if (!($page->hasChildren() && !$subMenu)) {
                $html .= sprintf('<li><div class="menu-glyph"%s><a id="%s" href="%s" class="%s">&nbsp;</a></div>%s</li>',
                        $this->getInlineStyle($page->getId()),
                        'menu-collapse-' . $this->getId($page),
                        $page->hasChildren() ? 'javascript:;' : $page->getHref(),
                        $class . " " . $page->getClass(),
                        $subMenu
                );
            }
        }
        return sprintf('<ul class="admin-menu-collapsed" style="display:none">%s</ul>%s',
                $html, "\n");
    }

    protected function renderSubMenu(Zend_Navigation_Page $page, $caption = '') {
        $html = '';
        $pages = $page->getPages();
        foreach ($pages as $subPage)
        {
            if ($this->acl && $subPage->getResource())
                if (!$this->acl->hasPermission($subPage->getResource(), $subPage->getPrivilege()))
                    continue;
            if ($subPage->isActive()) {
                $this->activePageId = $this->getId($subPage);
            }
            if (!$subPage->isVisible(true)) continue;
            if (!$subPage->getHref()) continue;
            $html .= sprintf('<li><a id="%s" href="%s" class="%s">%s</a></li>',
                    'menu-collapse-' . $this->getId($subPage),
                    $subPage->getHref(),
                    $subPage->getClass(),
                    $this->view->escape($subPage->getLabel())
            );
        }
        if ($html) {
            $html = sprintf('<li class="caption"><div class="menu-glyph-delimeter">%s</div></li>',
                    $this->view->escape($caption)) . $html;
        }

        return $html ? sprintf('<ul>%s</ul>', $html) : $html;
    }

    protected function getInlineStyle($id, $offset = 10) {

        $spriteOffset = Am_View::getSpriteOffset($id);
        if ($spriteOffset === false) return '';

        $realOffset = $offset - $spriteOffset;

        return sprintf(' style="background-position: %spx center;" ', $realOffset);
    }

    protected function getId(Zend_Navigation_Page $page) {
        $id = $page->getId();
        if (!empty($id)) return $id;
        if ($page instanceof Zend_Navigation_Page_Mvc)
            return sprintf('%s-%s', $page->getController(), $page->getAction());
        elseif ($page instanceof Zend_Navigation_Page_Uri)
            return crc32($page->getUri);
    }
}

